using System;
using Microsoft.SPOT;
using  Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace PomodoroTimer
{
    public class LedProgressBar
    {
        public bool Unexpected { get; set; }
    
        private readonly Cpu.Pin[] pinList = new Cpu.Pin[]
            {
                Pins.GPIO_PIN_D13,
                Pins.GPIO_PIN_D12,
                Pins.GPIO_PIN_D11,
                Pins.GPIO_PIN_D10,

                Pins.GPIO_PIN_D9,
                Pins.GPIO_PIN_D8,
                Pins.GPIO_PIN_D7,
                Pins.GPIO_PIN_D6
            };

        private readonly OutputPort[] ledBar = new OutputPort[8];

        private long progressValue = 0;

        public long valueToDisplay;

        public long ProgressValue {
            get
            {
                return this.progressValue;
            }
            
            set 
            {
                if (this.Min <= value && value <= this.Max)
                {
                    this.progressValue = value;
                }
                else if (value < this.Min)
                {
                    this.progressValue = this.Min;
                }
                else if (value > this.Max)
                {
                    this.progressValue = this.Max;
                }

                valueToDisplay = (int) this.Recount(this.progressValue);
            }
        }

        public long Min { get; set; }
        public long Max { get; set; }

        public LedProgressBar(long value)
        {
            this.InitLedBar();
            this.Min = 0;
            this.Max = 100;
            this.ProgressValue = value;
            this.Unexpected = false;
        }

        private void InitLedBar()
        {
            for (int n = 0; n < this.ledBar.Length; n++)
            {
                this.ledBar[n] = new OutputPort(this.pinList[n], false);
            }
        }

        private long Recount(long value)
        {
            long dilek;
            if (value > 0)
            {

                var interval = (this.Max - this.Min);
                dilek = (value * this.ledBar.Length) / interval;
            }
            else
            {
                dilek = 0;
            }
            
            return  dilek;
        }

        public void Display()
        {
            if (this.Unexpected)
            {
                this.WormMove();
            }
            else
            {
                if (valueToDisplay > this.ledBar.Length)
                {
                    throw new ArgumentOutOfRangeException("displayValue", "Hodnota je mimo rozsah");
                }

                //this.Clear();

                int startIdx = 0;
                for (int n = 0; n < valueToDisplay; n++)
                {
                    this.ledBar[n].Write(true);
                    startIdx = n;
                }

                for (int n = startIdx; n < this.ledBar.Length; n++)
                {
                    this.ledBar[n].Write(false);
                }
            }
        }

        //private void Clear()
        //{
        //    for (int n = 0; n < this.ledBar.Length; n++)
        //    {
        //        this.ledBar[n].Write(false);
        //    }
        //}

        private int idx = 0;

        private DateTime nextStep = DateTime.Now;

        private void WormMove()
        {
            if (nextStep < DateTime.Now)
            {
                this.ledBar[idx].Write(false);
                idx++;
                idx = idx % this.ledBar.Length;
                this.ledBar[idx].Write(true);
                nextStep = DateTime.Now.AddMilliseconds(100);    
            }
        }
    }
}
